//
//  CZVistorView.swift
//  czbkiosweibo5
//
//  Created by zhangping on 15/10/12.
//  Copyright © 2015年 zhangping. All rights reserved.
//

import UIKit

/*
    1.懒加载需要的控件
    2.添加到CZVistorView
    3.设置布局
        AutoLayout约束:要确定一个view的的约束.需要指定位置约束和尺寸约束
            位置约束分为: 水平方向(x)和垂直方向(y)
*/

/**
*  按钮点击代理方法
*/

// 协议需要继承NSObjectProtocol
protocol CZVistorViewDelegate: NSObjectProtocol {
    func vistorViewWillRegister()
    func vistorViewWillLogin()
}

class CZVistorView: UIView {
    
    // MARK: - 属性
    
    weak var vistorViewDelegate: CZVistorViewDelegate?

    // MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MAKR: - 按钮点击事件
    func willRegister() {
        // 如果 ? 前面没有值,不执行后面的语句, ? 前面有值才执行后面的语句
        vistorViewDelegate?.vistorViewWillRegister()
    }
    
    func willLogin() {
        vistorViewDelegate?.vistorViewWillLogin()
    }
    
    // MARK: - 公共方法
    /**
    首页小房子旋转动画
    */
    func startRotateAnimation() {
        // 创建动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        // 设置属性
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        
        // 动画添加到转轮上面
        iconView.layer.addAnimation(anim, forKey: "anim")
    }
    
    /**
    根据不同的控制器设置不同访客视图的内容
    */
    func setupInfo(imageName: String, message: String) {
        // 隐藏房子
        homeView.hidden = true
        
        // 遮盖放到最底部
        sendSubviewToBack(coverView)
        
        // 设置iconView图片
        iconView.image = UIImage(named: imageName)
        
        // 设置message
        messageLabel.text = message
        messageLabel.sizeToFit()
    }
    
    // MARK: - 准备UI
    /**
    准备UI
    */
    private func prepareUI() {
        // 背景颜色
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        // 添加子控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 设置布局
        
        // 设置自动约束一定要设置成false.
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        
        // iconView X方向
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // iconView Y方向
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        // 房子 
        // X方向
        addConstraint( NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // Y方向
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 消息label. CenterX和iconView.CenterX重合.,宽度224
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // 顶部距离iconView底部16
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 如果不参照其他控件toItem填nil. attribute填: NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        
        // 注册按钮
        // 顶部距离messageLabel 16
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 左边和messageLabel的左边重合
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        // 宽度 100
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        // 高度 35
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 注册按钮
        // 顶部距离messageLabel 16
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        // 左边和messageLabel的左边重合
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        // 宽度 100
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        // 高度 35
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        
        // 遮盖
        // X
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        
        // Y 顶部和父控件顶部重合
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        // 底部和registerButton底部重合
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    }

    // MARK:  - 懒加载控件
    // 转轮/消息/我/ 用于存放350*350的图片
    private lazy var iconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return UIImageView(image: image)
    }()
    
    // 房子
    private lazy var homeView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_house")
        
        return UIImageView(image: image)
    }()
    
    // 消息文字
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        // 设置文字
        label.text = "关注一些人,看看有什么惊喜"
        
        // 可以显示多行
        label.numberOfLines = 0
        
        label.textColor = UIColor.darkGrayColor()
        // 设置label适合文字尺寸
        label.sizeToFit()
        
        return label
    }()
    
    // 注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        
        // 设置按钮背景
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        // 设置按钮文字内容
        button.setTitle("注册", forState: UIControlState.Normal)
        
        // 设置文字颜色
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        // 添加店家事件
        button.addTarget(self, action: "willRegister", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    // 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        
        // 设置按钮背景
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        // 设置按钮文字
        button.setTitle("登录", forState: UIControlState.Normal)
        
        // 设置文字颜色
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        // 添加点击事件
        button.addTarget(self, action: "willLogin", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    // 半透明遮罩, 不要取 maskView.这样和系统的重复了
    private lazy var coverView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return UIImageView(image: image)
    }()
}
